package Plugin_Reprise.properties;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.PropertyPage;

import Plugin_Reprise.Activator;
import Plugin_Reprise.preferences.PreferenceConstants;
import Prog.Objets.ObjetUniverse;

public class SamplePropertyPage extends PropertyPage {

	private static final String PATH_TITLE = "Projet:";
	
	private static final String UV_TITLE = "&Compte UV:";
	public static final String UV_PROPERTY = "UV_ACCOUNT";
	private static String DEFAULT_ACCOUNT = "";
	
	private static final String UV_TITLE2 = "&Exec checksum:";
	public static final String UV_PROPERTY2 = "UV_EXECCHECK";
	private static String DEFAULT_ACCOUNT2 = "EXEC.SP FILECHECKSUM.MOLI";
	
	private static final String UV_TITLE3 = "&Suffixe Table Oracle:";
	public static final String UV_PROPERTY3 = "UV_SUFFIXETABLEORA";
	private static String DEFAULT_ACCOUNT3 = "P";


	private static final int TEXT_FIELD_WIDTH = 50;

	private Text uvText;
	private Text uvText2;
	private Text uvText3;
	/**
	 * Constructor for SamplePropertyPage.
	 */
	public SamplePropertyPage() {
		super();
		IPreferenceStore prefs =Activator.getDefault().getPreferenceStore();
		DEFAULT_ACCOUNT=(prefs.getString(PreferenceConstants.UV_Constants.UV_ACCOUNT));

	}

	private void addFirstSection(Composite parent) {
		Composite composite = createDefaultComposite(parent);

		//Label for path field
		Label pathLabel = new Label(composite, SWT.NONE);
		pathLabel.setText(PATH_TITLE);

		// Path text field
		Text pathValueText = new Text(composite, SWT.WRAP | SWT.READ_ONLY);
		pathValueText.setText(((IResource) getElement()).getFullPath().toString());
	}

	private void addSeparator(Composite parent) {
		Label separator = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		separator.setLayoutData(gridData);
	}

	private void addSecondSection(Composite parent) {
		Composite composite = createDefaultComposite(parent);

		// Label for owner field
		Label ownerLabel = new Label(composite, SWT.NONE);
		ownerLabel.setText(UV_TITLE);

		// Owner text field
		uvText = new Text(composite, SWT.SINGLE | SWT.BORDER);
		GridData gd = new GridData();
		gd.widthHint = convertWidthInCharsToPixels(TEXT_FIELD_WIDTH);
		uvText.setLayoutData(gd);

		// Populate owner text field
		try {
			String owner =
				((IResource) getElement()).getPersistentProperty(
					new QualifiedName("", UV_PROPERTY));
			uvText.setText((owner != null) ? owner : DEFAULT_ACCOUNT);
		} catch (CoreException e) {
			IPreferenceStore prefs =Activator.getDefault().getPreferenceStore();
			uvText.setText(prefs.getString(PreferenceConstants.UV_Constants.UV_ACCOUNT));
		}
		
		Label ownerLabel2 = new Label(composite, SWT.NONE);
		ownerLabel2.setText(UV_TITLE2);
		uvText2 = new Text(composite, SWT.SINGLE | SWT.BORDER);
		GridData gd2 = new GridData();
		gd2.widthHint = convertWidthInCharsToPixels(TEXT_FIELD_WIDTH);
		uvText2.setLayoutData(gd2);

		// Populate owner text field
		try {
			String owner =
				((IResource) getElement()).getPersistentProperty(
					new QualifiedName("", UV_PROPERTY2));
			uvText2.setText((owner != null) ? owner : DEFAULT_ACCOUNT2);
		} catch (CoreException e) {
			IPreferenceStore prefs =Activator.getDefault().getPreferenceStore();
			uvText2.setText(prefs.getString(PreferenceConstants.UV_Constants.UV_EXECCHECK));
		}
		
		Label ownerLabel3 = new Label(composite, SWT.NONE);
		ownerLabel3.setText(UV_TITLE3);
		uvText3 = new Text(composite, SWT.SINGLE | SWT.BORDER);
		GridData gd3 = new GridData();
		gd3.widthHint = convertWidthInCharsToPixels(TEXT_FIELD_WIDTH);
		uvText3.setLayoutData(gd3);

		// Populate owner text field
		try {
			String owner =
				((IResource) getElement()).getPersistentProperty(
					new QualifiedName("", UV_PROPERTY3));
			uvText3.setText((owner != null) ? owner : DEFAULT_ACCOUNT3);
		} catch (CoreException e) {
			
			uvText3.setText(DEFAULT_ACCOUNT3);
		}
		
	}

	/**
	 * @see PreferencePage#createContents(Composite)
	 */
	protected Control createContents(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		composite.setLayout(layout);
		GridData data = new GridData(GridData.FILL);
		data.grabExcessHorizontalSpace = true;
		composite.setLayoutData(data);

		addFirstSection(composite);
		addSeparator(composite);
		addSecondSection(composite);
		
	    final Button button0 = new Button(composite, SWT.NONE);
	    button0.setText("Test Connexion au compte Universe");
	    button0.addSelectionListener(new SelectionListener() {
	        public void widgetSelected(SelectionEvent arg0) {
	        	testConnectUV(button0.getShell());
	        }
	        public void widgetDefaultSelected(SelectionEvent arg0) {
	        }
	      });
	    
	    
		return composite;
	}

	private void testConnectUV(Shell shell){
    	ObjetUniverse objUV = new ObjetUniverse(null,null);
		if (objUV.testConnectUV(uvText.getText())){
			MessageDialog.openInformation(shell,
					"Test connexion Universe",
					"Connexion avec succes");
		} else {
			MessageDialog.openError(
					shell,
					"Test connexion Universe",
					"Erreur de connexion");
		}
	}
	
	private Composite createDefaultComposite(Composite parent) {
		Composite composite = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		composite.setLayout(layout);

		GridData data = new GridData();
		data.verticalAlignment = GridData.FILL;
		data.horizontalAlignment = GridData.FILL;
		composite.setLayoutData(data);


		
		return composite;
	}

	protected void performDefaults() {
		super.performDefaults();
		// Populate the owner text field with the default value
		IPreferenceStore prefs =Activator.getDefault().getPreferenceStore();
		uvText.setText(prefs.getString(PreferenceConstants.UV_Constants.UV_ACCOUNT));
		uvText2.setText(prefs.getString(PreferenceConstants.UV_Constants.UV_EXECCHECK));
		uvText3.setText("P");
	}
	
	public boolean performOk() {
		// store the value in the owner text field
		try {
			((IResource) getElement()).setPersistentProperty(
				new QualifiedName("", UV_PROPERTY),
				uvText.getText());
			((IResource) getElement()).setPersistentProperty(
					new QualifiedName("", UV_PROPERTY2),
					uvText2.getText());
			((IResource) getElement()).setPersistentProperty(
					new QualifiedName("", UV_PROPERTY3),
					uvText3.getText());
		} catch (CoreException e) {
			return false;
		}
		return true;
	}

}